<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="org.tobuy.vo.*" import="org.tobuy.factory.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    
    <title>My JSP 'History.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles_rate.css">

  </head>
  
  <body>
    <h4>Purchese History</h4>
   <table border=1>
   <tr>
   		<td>Item_name</td>
  		<td>Price</td>
  		<td>Seller_id</td>
  		<td>Rate this Seller</td>
   </tr>
   <%
   User buyer = (User)session.getAttribute("user");
   	List<Deal> deals = new ArrayList<Deal>();
   	deals = DealDAOFactory.getIDealDAOInstance().findBoughtDeal(buyer);
      	Iterator<Deal> iter = deals.iterator();
     		while(iter.hasNext()){
     		Deal deal = iter.next();
     		Item item = new Item();
     		item = ItemDAOFactory.getIItemDAOInstance().findItem(Integer.toString(deal.getItem_id()));
   %>
  		<tr>
  		<td><%=item.getItem_name() %></td>
  		<td><%=deal.getDeal_price() %></td>
  		<td><a href="ShowUserInfo?user_id=<%=deal.getSeller_id() %>"><%=deal.getSeller_id() %></a></td>
  		<td><% 
  			List<Score> scores = new ArrayList<Score>();
  			User seller = new User();
  			seller = UserDAOFactory.getIUserDAOInstance().findUser(deal.getSeller_id());
  			scores = ScoreDAOFactory.getIScoreDAOInstance().findSellerScoreSet(seller);
  			String score_info = new String();
			int sum_score = 0;
			int avg_score = 0;
			Iterator<Score> scoreIter = scores.iterator();
			while(scoreIter.hasNext()){
				Score score = scoreIter.next();
				sum_score = score.getScore_value()+sum_score;
			}
			if(scores.size()!=0){avg_score = sum_score/scores.size();}
		
			score_info = avg_score + "/" + scores.size();
			for(int i = 0; i<avg_score;i++){
			%>
				<a class="yellow" href="AddScore?score_value=<%=i+1 %>&score_type=seller&target_id=<%=seller.getUser_id() %>"></a>
			<%
			}
			for(int i =avg_score+1; i<6;i++){
			%>
				<a class="gray" href="AddScore?score_value=<%=i %>&score_type=seller&target_id=<%=seller.getUser_id() %>"></a>
			<%
			}
			%><%=score_info %></td>
  		</tr>
  		<%
  		}
    %>
    </table>
  </body>
</html>
